Crate numpy

source ·
Expand description

This crate provides Rust interfaces for NumPy C APIs, especially for the ndarray class.

It uses pyo3 for Rust bindings to CPython, and uses ndarray as the Rust matrix library.

To resolve its dependency on NumPy, it calls import numpy.core internally. This means that this crate should work if you can use NumPy in your Python environment, e.g. after installing it by pip install numpy. It does not matter whether you use the system environment or a dedicated virtual environment.

Loading NumPy is done automatically and on demand. So if it is not installed, the functions provided by this crate will panic instead of returning a result.

Integration with nalgebra is provided via an implementation of ToPyArray for nalgebra::Matrix to convert nalgebra matrices into NumPy arrays as well as the PyReadonlyArray::try_as_matrix and PyReadwriteArray::try_as_matrix_mut methods to treat NumPy array as nalgebra matrix slices.

Example

use numpy::pyo3::Python;
use numpy::ndarray::array;
use numpy::{ToPyArray, PyArray};

Python::with_gil(|py| {
    let py_array = array![[1i64, 2], [3, 4]].to_pyarray(py);

    assert_eq!(
        py_array.readonly().as_array(),
        array![[1i64, 2], [3, 4]]
    );
});
use numpy::pyo3::Python;
use numpy::nalgebra::Matrix3;
use numpy::{pyarray, ToPyArray};

Python::with_gil(|py| {
    let py_array = pyarray![py, [0, 1, 2], [3, 4, 5], [6, 7, 8]];

    let py_array_square;

    {
        let py_array = py_array.readwrite();
        let mut na_matrix = py_array.as_matrix_mut();

        na_matrix.add_scalar_mut(1);

        py_array_square = na_matrix.pow(2).to_pyarray(py);
    }

    assert_eq!(
        py_array.readonly().as_matrix(),
        Matrix3::new(1, 2, 3, 4, 5, 6, 7, 8, 9)
    );

    assert_eq!(
        py_array_square.readonly().as_matrix(),
        Matrix3::new(30, 36, 42, 66, 81, 96, 102, 126, 150)
    );
});

Re-exports

Modules

  • Safe interface for NumPy’s N-dimensional arrays
  • Types to safely create references into NumPy arrays
  • Defines conversion traits between Rust types and NumPy data types.
  • Support datetimes and timedeltas
  • Low-Level bindings for NumPy C API.

Macros

  • Create an Array with one, two or three dimensions.
  • Return the Einstein summation convention of given tensors.
  • Create a PyArray with one, two or three dimensions.

Structs

Enums

Traits

  • Represents that a type can be an element of PyArray.

Functions

  • Create a one-dimensional index
  • Create a two-dimensional index
  • Create a three-dimensional index
  • Create a four-dimensional index
  • Create a five-dimensional index
  • Create a six-dimensional index
  • Create a dynamic-dimensional index
  • Return the dot product of two arrays.
  • Returns the type descriptor (“dtype”) for a registered type.
  • Return the Einstein summation convention of given tensors.
  • Return the inner product of two arrays.

Type Aliases

  • one-dimensional
  • two-dimensional
  • three-dimensional
  • four-dimensional
  • five-dimensional
  • six-dimensional
  • dynamic-dimensional
  • Receiver for zero-dimensional arrays or array-like types.
  • Receiver for one-dimensional arrays or array-like types.
  • Receiver for two-dimensional arrays or array-like types.
  • Receiver for three-dimensional arrays or array-like types.
  • Receiver for four-dimensional arrays or array-like types.
  • Receiver for five-dimensional arrays or array-like types.
  • Receiver for six-dimensional arrays or array-like types.
  • Receiver for arrays or array-like types whose dimensionality is determined at runtime.